﻿using IceDog.EFCore.Cmd.OfficalDemo.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IceDog.EFCore.Cmd.OfficalDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SaveData();
            QueryData();
            Console.ReadKey();
        }
        static void SaveData()
        {
            //保存数据
            using (var db = new DioBlogContext())
            {
                var guid = Guid.NewGuid().ToString("N");
                var ran = new Random();
                var blog = new Blog
                {
                    Url = "http://www.example.com/tim/" + guid,
                    Rating = ran.Next(1, 10),
                    CreateTime = DateTime.Now
                };
                var posts = new List<Post>()
                {
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="good"
                    },
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="good"
                    },
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="good"
                    }
                };
                blog.Posts = posts;
                db.Blogs.Add(blog);
                guid = Guid.NewGuid().ToString("N");
                blog = new Blog
                {
                    Url = "http://www.example.com/jane/" + guid,
                    Rating = ran.Next(1, 10),
                    CreateTime = DateTime.Now
                };
                posts = new List<Post>()
                {
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="bad"
                    },
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="bad"
                    },
                    new Post()
                    {
                        Blog=blog,
                        Title="arm",
                        Content="bad"
                    }
                };
                blog.Posts = posts;
                db.Blogs.Add(blog);
                var count = db.SaveChanges();
                Console.WriteLine("保存了{0}条数据", count);
            }
        }

        static void QueryData()
        {
            //查询
            using (var db = new DioBlogContext())
            {
                var blogs = db.Blogs
                    .OrderBy(b => b.CreateTime)
                    .Include(b=>b.Posts)
                    .AsNoTracking()
                    .ToList();
                var settings = new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                };
                Console.WriteLine("评分大于3分的博客数据\r\n"+JsonConvert.SerializeObject(blogs,Formatting.Indented, settings));
            }
        }
    }
}
